﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
using BC;
using BE;
using COMExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Threading;
namespace ModuleDuoc.Report
{
    public partial class frmTK_SuDungThuoc : ModuleDuoc.Base.frmTK_Base
    {
        string sql;
        Zd_BienBanKiemKe be = new Zd_BienBanKiemKe();
        Zd_BienBanKiemKeBC bc = new Zd_BienBanKiemKeBC();
        public frmTK_SuDungThuoc()
        {
            InitializeComponent();
        }

        private void frmTK_SuDungThuoc_Load(object sender, EventArgs e)
        {
            this.dgvData.AutoGenerateColumns = false;
            dateThoiGian.Text = DateTime.Parse(this.dateThoiGian.Value.ToString("MM/yyyy")).ToString();
        }

        private void insertData()
        {
            string sql = "zD_BC_TONKHO";
            //string sql = "zD_BC_KiemKeThuoc";
            int thang = Int32.Parse(dateThoiGian.Text.Substring(0, 2));
            int nam = Int32.Parse(dateThoiGian.Text.Substring(3, 4));

            System.Data.SqlClient.SqlParameter tungay = new System.Data.SqlClient.SqlParameter();
            tungay.ParameterName = "@tungay";
            tungay.DbType = DbType.DateTime;
            tungay.Direction = ParameterDirection.Input;
            tungay.Value = "01/" + dateThoiGian.Text +" 00:00:00";

            System.Data.SqlClient.SqlParameter denngay = new System.Data.SqlClient.SqlParameter();
            denngay.ParameterName = "@denngay";
            denngay.DbType = DbType.DateTime;
            denngay.Direction = ParameterDirection.Input;
            denngay.Value = DateTime.DaysInMonth(nam, thang) + "/" + dateThoiGian.Text + " 23:59:00";
            System.Data.SqlClient.SqlParameter mavt;

            mavt = new System.Data.SqlClient.SqlParameter("@mavt", "");
            System.Data.SqlClient.SqlParameter manhom;
            manhom = new System.Data.SqlClient.SqlParameter("@manhom", "");
            System.Data.SqlClient.SqlParameter nhacc = new System.Data.SqlClient.SqlParameter("@nhacc", "");
            System.Data.SqlClient.SqlParameter wherein = new System.Data.SqlClient.SqlParameter("@wherein", "");
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.StoredProcedure, sql,
                tungay,
                denngay,
                mavt,
                manhom,
                nhacc,
                wherein);
        }

        /// <summary>
        /// Xem kết quả
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnView_Click(object sender, EventArgs e)
        {
            base.btnView_Click(sender, e);
            insertData(); // Đưa dữ liệu vào bảng tạm
            sql = "SELECT zdbt.MAHANGHOA, zdbt.tenvt, zdbt.dvt, AVG(zdbt.dongiaxuat) AS GiaTB, " +
                    "sum(zdbt.slxuat) AS SLXuat, sum(zdbt.tienxuat) AS TienXuat, " +
                    "sum(zdk.slhong) AS slhong, sum(zdbt.dongianhap * slhong) AS tienhong, " +
                    "sum(zdbt.slxuat) - sum(slhong) AS sltong, sum(zdbt.tienxuat) - sum(zdbt.dongianhap*slhong) AS tientong " +
                    "FROM zD_bcTonkhoth zdbt " +
                    "INNER JOIN zD_KIEMKE2012 zdk ON zdk.mavach = zdbt.mavach " +
                    "WHERE zdbt.slxuat <> 0 AND zdbt.manhom = 60 " +
                    "GROUP BY zdbt.MAHANGHOA, zdbt.tenvt, zdbt.dvt " +
                    "ORDER BY zdbt.MAHANGHOA";
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            dgvData.DataSource = dt;
            lblSoLuong.Text = "Số lượng: " + dt.Rows.Count.ToString();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                try
                {
                    dgvData.Rows[i].Cells["sltong"].Value = Int32.Parse(dgvData.Rows[i].Cells["SLXuat"].Value.ToString()) - Int32.Parse(dgvData.Rows[i].Cells["slhong"].Value.ToString());
                    dgvData.Rows[i].Cells["tientong"].Value = Int32.Parse(dgvData.Rows[i].Cells["TienXuat"].Value.ToString()) - Int32.Parse(dgvData.Rows[i].Cells["tienhong"].Value.ToString());
                }
                catch { }
            }
        }

        /// <summary>
        /// Xuất Excel
        /// </summary>s
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnExcel_Click(object sender, EventArgs e)
        {
           // xuatExcel();
        }

        public override void btnExportReport_Click(object sender, EventArgs e)
        {
            base.btnExportReport_Click(sender, e);
            insertData(); // Đưa dữ liệu vào bảng tạm
            CapNhat();
            printReport();
        }
        private void CapNhat() // dùng chung với bảng Biên bản kiểm kê
        {
            be.BoYTe = txtBoYte.Text;
            be.BenhVien = txtBenhVien.Text;
            be.MS = txtMS.Text;
            be.So = txtSo.Text;
            be.TinhTrang = "1";
            be.Field5 = "SuDungThuoc";

            if (txtKiemKe_ID.Text == "")
            {
                bc.Insert(be);
                //MessageBox.Show("Thêm mới thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                // be.ThoiGian = dateThoiGian.Value.ToString("MM/yyyy");//thông kê theo thang
                be.KiemKe_ID = Int32.Parse(txtKiemKe_ID.Text);
                bc.Update(be);
                //MessageBox.Show("Cập nhật thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void printReport()
        {
            sql = "SELECT zdbt.MAHANGHOA, zdbt.tenvt, zdbt.dvt, AVG(zdbt.dongiaxuat) AS GiaTB, " +
                    "sum(zdbt.slxuat) AS SLXuat, sum(zdbt.tienxuat) AS TienXuat, " +
                    "sum(zdk.slhong) AS slhong, sum(zdbt.dongianhap * slhong) AS tienhong, " +
                    "sum(zdbt.slxuat) - sum(slhong) AS sltong, sum(zdbt.tienxuat) - sum(zdbt.dongianhap*slhong) AS tientong " +
                    "FROM zD_bcTonkhoth zdbt " +
                    "INNER JOIN zD_KIEMKE2012 zdk ON zdk.mavach = zdbt.mavach " +
                    "WHERE zdbt.slxuat <> 0 AND zdbt.manhom = 60 " +
                    "GROUP BY zdbt.MAHANGHOA, zdbt.tenvt, zdbt.dvt " +
                    "ORDER BY zdbt.MAHANGHOA";
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            frmShowReport frm = new frmShowReport(2);
            frmShowReport.strC = frmShowReport.BC.BAOCAOSUDUNGTHUOC;
            frmShowReport._dt = dt;
            frmShowReport._thoiGian = dateThoiGian.Text;
            frmShowReport._boYte = txtBoYte.Text;
            frmShowReport._benhVien = txtBenhVien.Text;
            frmShowReport._ms = txtMS.Text;
            frmShowReport._so = txtSo.Text;
            //frmShowReport._sltong = txtMS.Text;
            //frmShowReport._tientong = txtSo.Text;
            frm.ShowDialog();
        }

        private void dateThoiGian_ValueChanged(object sender, EventArgs e)
        {
            fillAll();
        }

        private void fillAll()
        {
            sql = "SELECT KiemKe_ID, BoYTe, BenhVien, Khoa, MS, So, Ten1, Ten2, Ten3, Ten4, Ten5, " +
                            "ChucDanh1, ChucDanh2, ChucDanh3, ChucDanh4, ChucDanh5, Date1, Date2, ThoiGian, DeXuat " +
                            "FROM zD_BienBanKiemKe " +
                            "WHERE Field5 = 'SuDungThuoc' and thoiGian = '" + dateThoiGian.Text + "'";

            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            //this.dgvData.DataSource = dt;

            if (dt.Rows.Count > 0)
            {
                txtKiemKe_ID.Text = dt.Rows[0]["KiemKe_ID"].ToString();
                txtBoYte.Text = dt.Rows[0]["BoYTe"].ToString();
                txtBenhVien.Text = dt.Rows[0]["BenhVien"].ToString();
                txtMS.Text = dt.Rows[0]["MS"].ToString();
                txtSo.Text = dt.Rows[0]["So"].ToString();
                be.ThoiGian = dateThoiGian.Value.ToString("MM/yyyy");//thông kê theo thang           
            }
            else
            {
                txtKiemKe_ID.Text = "";
            }
        }
    }
}
